/*
 * jxSwing Project (2014)
 * 
 * by Denilson Edinaldo Pinto
 * 
 * LGPL
 * Copyright (C) 2014
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package org.jxSwing.component.panel;

import java.awt.BorderLayout;
import java.awt.Color;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.UIManager;
import javax.swing.border.LineBorder;
import javax.swing.table.DefaultTableModel;

import org.jxSwing.Cleanable;
import org.jxSwing.component.JXComponent;
import org.jxSwing.component.bar.FooterTableBar;
import org.jxSwing.component.table.LineNumberTable;
import org.jxSwing.component.table.ResultSetTable;
import org.jxSwing.i18n.I18N;
import org.jxUtils.check.CheckUtil;

/**
 * @author Denilson Edinaldo Pinto
 * @porject jxSwing
 * @package org.jxSwing.component.panel
 * @date 19/09/2014 - 20:51:15
 */
public class ResultSetTablePanel extends JPanel implements JXComponent, Cleanable {
	
	private static final long serialVersionUID = -8411583832041209389L;
	
	private JScrollPane scrollPane;
	
	private final ResultSetTable table;
	
	private FooterTableBar footerBar;
	
	/**
	 */
	public ResultSetTablePanel() {
		
		table = new ResultSetTable();
		
		initGUI();
	}
	
	/**
	 * @param resultSet
	 * @throws SQLException
	 */
	public ResultSetTablePanel(final ResultSet resultSet) throws SQLException {
	
		table = new ResultSetTable(resultSet);
		
		initGUI();
	}
	
	/**
	 *
	 */
	private void initGUI() {
	
		setLayout(new BorderLayout());
		
		add(getScrollPane(), BorderLayout.CENTER);
		add(getFooterBar(), BorderLayout.SOUTH);
	}
	
	@Override
	public final void clear() {
	
		getTable().clear();
	}
	
	/**
	 * @return the footerBar
	 */
	public final FooterTableBar getFooterBar() {
	
		if(footerBar == null) {
			footerBar = new FooterTableBar(getTable());
		}
		return footerBar;
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jxSwing.component.JXComponent#getIndex()
	 */
	@Override
	public final int getIndex() {
	
		return getTable().getIndex();
	}
	
	/**
	 * @return model
	 */
	public final DefaultTableModel getModel() {
	
		return getTable().getModel();
	}
	
	/**
	 * @return the number of rows
	 */
	public final int getRowCount() {
	
		return getTable().getRowCount();
	}
	
	/**
	 * @return the scrollPane
	 */
	public final JScrollPane getScrollPane() {
	
		if(scrollPane == null) {
			scrollPane = new JScrollPane(getTable());
			scrollPane.setRowHeaderView(new LineNumberTable(scrollPane, getTable()));
		}
		return scrollPane;
	}
	
	/**
	 * @return resultSetTable
	 */
	public final ResultSetTable getTable() {
	
		return table;
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jxSwing.component.JXComponent#isAtributte()
	 */
	@Override
	public final boolean isAtributte() {
	
		return getTable().isAtributte();
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jxSwing.component.JXComponent#isRequired()
	 */
	@Override
	public final boolean isRequired() {
	
		return getTable().isRequired();
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jxSwing.component.JXComponent#isRequiredBorderEnabled()
	 */
	@Override
	public final boolean isRequiredBorderEnabled() {
	
		return getTable().isRequiredBorderEnabled();
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jxSwing.component.JXComponent#setAtributte(boolean)
	 */
	@Override
	public final void setAtributte(final boolean atributte) {
	
		getTable().setAtributte(atributte);
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jxSwing.component.JXComponent#setIndex(int)
	 */
	@Override
	public final void setIndex(final int index) {
	
		getTable().setIndex(index);
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see java.awt.Component#setName(java.lang.String)
	 */
	@Override
	public final void setName(final String name) {
	
		super.setName(name);
		
		setRequired(isRequired());
	}
	
	@Override
	public final void setRequired(final boolean required) {
	
		getTable().setRequiredBorderEnabled(false);
		getTable().setRequired(required);
		
		//
		if(required) {
			
			//
			if(isRequiredBorderEnabled()) {
				
				setBorder(new LineBorder(Color.RED));
			}
			
			//
			if(CheckUtil.isNullOrAbsoluteEmpty(getName())) {
				
				setToolTipText(I18N.component().requiredThis());
				
			}else {
				
				setToolTipText(I18N.component().requiredField(getName()));
			}
			
		}else {
			
			setBorder(UIManager.getBorder("Panel.border"));
			setToolTipText(null);
		}
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jxSwing.component.JXComponent#setRequiredBorderEnabled(boolean)
	 */
	@Override
	public final void setRequiredBorderEnabled(final boolean requiredBorderEnabled) {
	
		getTable().setRequiredBorderEnabled(requiredBorderEnabled);
	}
	
	/**
	 * @param resultSet
	 * @throws SQLException
	 */
	public final void setResultSet(final ResultSet resultSet) throws SQLException {
		
		getTable().setResultSet(resultSet);
		
		getFooterBar().setRows(getRowCount(), getRowCount());
	}
}
